/**
 * nghilt
 * 14-10-2011
 *
 * Copyright by N&N.,Inc
 */
package at.com.vn.chess.service;

import java.util.ArrayList;

import vn.com.bip.office.smart.util.XMLHelper;
import at.com.vn.chess.entity.ChessBoardInfo;
import at.com.vn.chess.entity.PlayersInfo;
import at.com.vn.chess.util.Const;

/**
 * 
 */
public class PlayShogiService extends ShogiService {

	public PlayShogiService( String inputStrXml ) {
		super( inputStrXml );
		logger.info( "Bat dau" );
	}	

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.com.vn.chess.service.BaseService#doService()
	 */
	@Override
	public void doService() {
		
		logger.info( "doService add PlayShogiService" );

		// Create initation xml error message
		String errMessage = "Some error";
		outputStrXml = XMLHelper
				.convertMessageToXML( "PlayShogi", "ERR_MSG", "MSG", errMessage );

		if ( inputStrXml.trim().equals( "" ) ) {
			return;
		}

		outputStrXml = inputStrXml;

		String processName = XMLHelper.getProcessName( inputStrXml );

		// Xu ly khi join vao mot table
		if ( processName.contains( "_join" ) ) {

			ChessBoardInfo info = ( ChessBoardInfo ) XMLHelper
					.convertXMLToListObject( inputStrXml, ChessBoardInfo.class )
					.get( 0 );

			boolean isExistTable = false;
			int pos = getPositionOfTableInListTable( chessBoardInfoList, info );
			if( pos < chessBoardInfoList.size() ) {
				isExistTable = true;
				outputStrXml = chessBoardInfoList.get( pos );
			}

			// Neu chua co thi them mot ban vao
			if ( !isExistTable ) {
				chessBoardInfoList.add( inputStrXml );
			}
		} else if ( processName.contains( "_getHistory" ) ) {
			getHistoryList();
		} else if ( processName.contains( "_play" ) ) {
			shogiProcess();
		} else if( processName.contains( "_sit_to_table" ) ) {
			doSitOrLeftOnToTableProcess( false );
		} else if( processName.contains( "_left_to_table" ) ) {
			doSitOrLeftOnToTableProcess( true );
		} else if( processName.contains( "_win_lose_action" ) ) {
			processWinLoseAction();
		} else if( processName.contains( "_reset_board" ) ) {
			resetBoard();
		}
	}

	@SuppressWarnings( "unchecked" )
	private void resetBoard() {
		ArrayList<ChessBoardInfo> listInfo = ( ArrayList<ChessBoardInfo> ) XMLHelper
				.convertXMLToListObject( inputStrXml, ChessBoardInfo.class );
		String processName = XMLHelper.getProcessName( inputStrXml );
		
		String player1Name = listInfo.get( 0 ).getBName();
		String player2Name = listInfo.get( 0 ).getWName();
		
		// Neu la chu ban thoat ra ngoai thi nguoi kia se tro thanh chu ban moi
		if( listInfo.get( 0 ).getIsTOwner() == Const.TRUE ) {				
			player1Name = player2Name;
		}
		
		player2Name = "";
		
		for( int j = 0; j < listInfo.size(); j++ ) {
			listInfo.get( j ).setBName( player1Name );
			listInfo.get( j ).setWName( player2Name );	
		}
		
		int pos = getPositionOfTableInListTable( chessBoardInfoList, listInfo.get(  0  ) );
		if( pos < chessBoardInfoList.size() ) {
			chessBoardInfoList.remove( pos );
			chessBoardInfoList.add( XMLHelper.convertListObjectToXML( listInfo, processName, ChessBoardInfo.class ) );
		}else {
			new RuntimeException();
		}		
	}

	private void processWinLoseAction() {
		PlayersInfo playersInfo = ( PlayersInfo ) XMLHelper
				.convertXMLToListObject( inputStrXml, PlayersInfo.class )
				.get( 0 );
		
		String player1Name = playersInfo.getPlayer1Name();
		String player2Name = playersInfo.getPlayer2Name();
				
		if( playersInfo.getWhosWon() != Const.UNDEFINE_VALUE ) {
			processWinLose( playersInfo.getWhosWon(), player1Name, player2Name, playersInfo.getRId(), playersInfo.getTId() );
		}
	}

	@SuppressWarnings( "unchecked" )
	private void doSitOrLeftOnToTableProcess( boolean isLeft ) {
		PlayersInfo playersInfo = ( PlayersInfo ) XMLHelper
				.convertXMLToListObject( inputStrXml, PlayersInfo.class )
				.get( 0 );
		
		String roomId = playersInfo.getRId();
		int tableId = playersInfo.getTId();
		
		String xmlTable = "";

		// Tim ban trong list
		int pos = getPositionOfTableInListTable( chessBoardInfoList, roomId, tableId );		
		if( pos < chessBoardInfoList.size() ) {
			xmlTable = chessBoardInfoList.get( pos );			
		}else {
			new RuntimeException();
		}
		
		// Neu la thoat ra ngoai thi xu ly thang thua
		if( isLeft ) {

			String player1IdBeforeChanged = playersInfo.getPlayer1Name();
			String player2IdBeforeChanged = playersInfo.getPlayer2Name();
			
			// Neu la chu ban thoat ra ngoai thi nguoi kia se tro thanh chu ban moi
			if( playersInfo.getIsOwnerAct() == Const.TRUE ) {				
				playersInfo.setPlayer1Name( playersInfo.getPlayer2Name() );
			}
			
			playersInfo.setPlayer2Name( "" );
			
			if( playersInfo.getWhosWon() != Const.UNDEFINE_VALUE ) {
				processWinLose( playersInfo.getWhosWon(), player1IdBeforeChanged, player2IdBeforeChanged, playersInfo.getRId(), playersInfo.getTId() );
			}
			
			String proName = XMLHelper.getProcessName( inputStrXml );
			outputStrXml = XMLHelper.convertListObjectToXML( playersInfo, proName, PlayersInfo.class );
		}
		
		ArrayList<ChessBoardInfo> listInfo = ( ArrayList<ChessBoardInfo> ) XMLHelper
				.convertXMLToListObject( xmlTable, ChessBoardInfo.class );
		String processName = XMLHelper.getProcessName( xmlTable );
		
		for( int j = 0; j < listInfo.size(); j++ ) {
			listInfo.get( j ).setBName( playersInfo.getPlayer1Name() );
			listInfo.get( j ).setWName( playersInfo.getPlayer2Name() );	
		}
		
		chessBoardInfoList.remove( pos );
		chessBoardInfoList.add( XMLHelper.convertListObjectToXML( listInfo, processName, ChessBoardInfo.class ) );
	}

	public void getHistoryList() {
		ChessBoardInfo info = ( ChessBoardInfo ) XMLHelper
				.convertXMLToListObject( inputStrXml, ChessBoardInfo.class )
				.get( 0 );

		int pos = getPositionOfTableInListTable( chessBoardHistoryList, info );
		if( pos < chessBoardHistoryList.size() ) {
			outputStrXml = chessBoardHistoryList.get( pos );
		}
	}
}
